package com.superlz.demo.security.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.superlz.demo.security.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 自定义验证注解-逻辑处理类
 *
 * 1.实现ConstraintValidator接口,<使用的验证注解,要验证的字段类型>
 * 2.重写对应的方法
 *
 * ConstraintValidator会自动被Spring管理
 *
 * @Author: lz
 * @Description:
 * @Date: 2019/6/28
 */
public class MyConstraintValidator implements ConstraintValidator<MyConstraint, Object> {

	@Autowired
	private HelloService helloService;

    /**
     * 初始化验证执行的动作
     * @param constraintAnnotation
     */
	@Override
	public void initialize(MyConstraint constraintAnnotation) {
		System.out.println("my validator init");
	}

    /**
     * 验证逻辑
     * @param value 校验的值
     * @param context 上下文消息
     * @return true为通过
     */
	@Override
	public boolean isValid(Object value, ConstraintValidatorContext context) {
		String greeting = helloService.greeting("tom");
		System.out.println(greeting);
		System.out.println(value);
		return false;
	}

}
